using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Reflection;

namespace SBPweb.Themes.Windows.Schemes
{
	/// <summary>
	/// Class of StyleSheetParser
	/// </summary>
    public class StyleSheetParser
    {
        #region Private fields

        private SortedList<string, StyleSheetClass> pScc;
        private StyleSheetBindingBase pSSB;

        #endregion

        #region Constructor logic

		/// <summary>
		/// Constructor of StyleSheetParser
		/// </summary>
		/// <param name="css">css</param>
		/// <param name="binding">style sheet binding base</param>
        public StyleSheetParser(string css, StyleSheetBindingBase binding)
        {
            this.pScc = new SortedList<string, StyleSheetClass>();
            this.pSSB = binding;
            FillStyleClass(css);
        }

        #endregion

        #region Property accessors

		/// <summary>
		/// List of classes
		/// </summary>
        public IList<string> ClassNames
        {
            get
            {
                return pScc.Keys;
            }
        }

        #endregion

        #region Methods

        #region Public methods

		/// <summary>
		/// Apply the style sheets
		/// </summary>
		/// <param name="className">classname</param>
		/// <param name="control">control</param>
        public void ApplyStyleSheets(string className, Control control)
        {
            if(!pScc.ContainsKey(className))
            {
                return;
            }

            ApplyAttributes(control, pScc[className].Attributes);
        }

        #endregion

        #region Private helper functions

        private void FillStyleClass(string s)
        {
            StyleSheetClass sc = null;
            string[] parts = s.Split(new char[] { '{', '}' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < parts.Length; i += 2 )
            {
                string styleName = CleanUp(parts[i]).Trim();
                if (this.pScc.ContainsKey(styleName))
                {
                    sc = this.pScc[styleName];
                    this.pScc.Remove(styleName);
                }
                else
                {
                    sc = new StyleSheetClass();
                }
                sc.Name = styleName;

                string[] atrs = CleanUp(parts[i + 1]).Split(';');
                foreach (string a in atrs)
                {
                    if (a.Contains(":"))
                    {
                        string _key = a.Split(':')[0].Trim().ToLower();
                        if (sc.Attributes.ContainsKey(_key))
                        {
                            sc.Attributes.Remove(_key);
                        }
                        sc.Attributes.Add(_key, a.Split(':')[1].Trim());
                    }
                }
                this.pScc.Add(sc.Name, sc);
            }
        }

        private string CleanUp(string s)
        {
            string temp = s;
            string reg = "(/\\*(.|[\r\n])*?\\*/)|(//.*)";
            Regex r = new Regex(reg);
            temp = r.Replace(temp, "");
            temp = temp.Replace("\r", "").Replace("\n", "").Replace("\t", " ");
            return temp;
        }

        private void ApplyAttributes(Control control, SortedList<string, string> attributes)
        {
            foreach (KeyValuePair<string, string> kvp in attributes)
            {
                pSSB.SetValue(kvp, control);
            }
        }

        #endregion

        #endregion
    }
}